package tests;

import processing.core.PApplet;
import processing.core.PVector;

public class PointField extends PApplet{
	float offset = 0;
	float offset2 = 0;
	
	public void setup(){
		size(800,640);
		noStroke();
		System.out.print( getx_Polar(10, 45) );
		System.out.print( " " + gety_Polar(10, 45) );
		
	}
	
	public void draw() {		
		background(150,150,150);
		PVector v1 = new PVector(300, 300); //y, x
		PVector v2 = new PVector(200, 200); //y, x
//		generateAreaPoints(v1, v2, false);
		

//		int[] resu = getPointsfromCirlce(100, (offset+=0.01) % 360);
//		int[] resu2 = getPointsfromCirlce(200, (offset2+=0.05) % 360);
//		
//		v1 = new PVector(resu2[0], resu2[1]); //y, x
//		v2 = new PVector(resu[0], resu[1]); //y, x
//		generateAreaPoints(v1, v2, false);


		
		
		
	}
	
	public void generateAreaPoints(PVector v1, PVector v2, boolean areRealValues){
		ellipseMode(CENTER);
		fill( color(0,0,0) );
		
		PVector p1 = v1;
		PVector p2 = v2;
		
		if(areRealValues){
//			context.convertRealWorldToProjective(v1, p1);
//			context.convertRealWorldToProjective(v2, p2);
		}
		ellipse(p1.x, p1.y, 20, 20);
		ellipse(p2.x, p2.y, 20, 20);
		  
		//winkel errechnen
		double angle = Math.atan2( (p2.y-p1.y), (p2.x-p1.x) )*180/Math.PI;

		
		int interate = 5;
		for (int i = 0; i <= interate; i++) {
			double x = 0;
			double y = 0;
			
			if(p2.y-p1.y > 0 && p2.x-p1.x > 0){
				//System.out.println("lo nach ru");
				y = Math.abs(p2.y-p1.y)/interate*i + getMin(p1.y, p2.y);
				x = Math.abs(p2.x-p1.x)/interate*i + getMin( p1.x, p2.x);
			}else if(p2.y-p1.y > 0 && p2.x-p1.x <= 0){
				//System.out.println("lu nach ro");
				y = Math.abs(p2.y-p1.y)/interate*i + getMin(p1.y, p2.y);
				x = -Math.abs(p2.x-p1.x)/interate*i + getMax( p1.x, p2.x);
			}else if(p2.y-p1.y <= 0 && p2.x-p1.x <= 0){
				y = Math.abs(p2.y-p1.y)/interate*i + getMin(p1.y, p2.y);
				x = Math.abs(p2.x-p1.x)/interate*i + getMin( p1.x, p2.x);
			}else if(p2.y-p1.y <= 0 && p2.x-p1.x > 0){
				y = Math.abs(p2.y-p1.y)/interate*i + getMin(p1.y, p2.y);
				x = -Math.abs(p2.x-p1.x)/interate*i + getMax( p1.x, p2.x);
			}
			fill( color(255,255,255) );
			ellipse( (float)x, (float)y,  10, 10);
			
			fill( color(255,0,255) );
			
			int x_offs = getx_Polar(20, angle+90);
			int y_offs = gety_Polar(20, angle+90);
			
			ellipse( (float)(x+x_offs), (float)(y+y_offs),  5, 5);
			ellipse( (float)(x-x_offs), (float)(y-y_offs),  5, 5);
			
		}

		
	}
	
	private int getx_Polar(double radius, double angle){
		return (int) (radius*(Math.cos(angle/360*2*Math.PI)) );
	}
	
	private int gety_Polar(double radius, double angle){
		return (int) (radius*(Math.sin(angle/360*2*Math.PI)) );
	}
	
	private float getMin(float a, float b){
		if(a > b){
			return b;
		}else{
			return a;
		}
	}
	
	private float getMax(float a, float b){
		if(a > b){
			return a;
		}else{
			return b;
		}
	}
	
	private int[] getPointsfromCirlce(float radius, float angle) { 
		int[] resu = new int[2];
		int i, winkel; 

		resu[0] = (int)(300  + Math.round(Math.cos(angle)* radius));
		resu[1] = (int)(300 - Math.round(Math.sin(angle)* radius));
		return resu;
	}


}
